#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from fuc import *
import requests

wifi = mac_switch_wifi('mobiletest')
user = got_user()
print("==> 当前用户", user['usr'])
print("==> 强制重签？", user['force'])

nowTime = got_now_time()
isWorkDay = is_work_day(nowTime)
isBefore9h = now_is_before_9h(nowTime)
isAfter18h = now_is_after_18h(nowTime)
print("==> 当前时间", nowTime, color(isWorkDay, "工作日", "休息日"), color(isBefore9h, "9点前", "9点后"), color(isAfter18h, "18点后", "18点前"))

driver = user_login(user)
print("==> 登陆结果")
cookie = cookie_check(driver)
print(cookie)

his = got_his_array(driver)
isSignin = is_signin(his)
isSignout = is_signout(his)
isSignlog = is_signlog(his)
print("==> 已发事项共", len(his), "条", color(isSignin, "今日已签到", "今日未签到"), color(isSignout, "今日已签退", "今日未签退"), color(isSignlog, "今日已填日志", "今日未填日志"))

tmp = got_my_template(driver)
print("==> 我的模板共", tmp['len'], "个，考勤签到:", tmp['signin'], "考勤签退:", tmp['signout'], "外包日志填写:", tmp['signlog'])

do_it = False
if isWorkDay and isBefore9h and (not isSignin or user['force']):
    print("==> 开始签到")
    do_it = True
    do_signin(driver, tmp['signin'])

if isWorkDay and isAfter18h and (not isSignout or user['force']):
    print("==> 开始签退")
    do_it = True
    do_signout(driver, tmp['signout'])

if isWorkDay and isAfter18h and not isSignlog:
    print("==> 开始填日志")
    do_it = True
    do_signlog(driver, tmp['signlog'], u"9点", u"180", u"开发")
    do_signlog(driver, tmp['signlog'], u"13点", u"300", u"开发")

if do_it:
    print("==> 查结果")
    his = got_his_array(driver)
    isSignin = is_signin(his)
    isSignout = is_signout(his)
    isSignlog = is_signlog(his)
    print(color(isSignin, "今日已签到", "今日未签到"), color(isSignout, "今日已签退", "今日未签退"), color(isSignlog, "今日已填日志", "今日未填日志"))

user_logout(driver)
print("==> 已登出")

mac_switch_wifi(wifi)
driver.quit()
